package cm.zhentao.controller;

import cm.zhentao.entity.Result;
import cm.zhentao.pojo.TDevice;
import cm.zhentao.pojo.TDeviceGather;
import cm.zhentao.pojo.TDeviceType;
import cm.zhentao.pojo.dto.DeviceDto;
import cm.zhentao.pojo.dto.DeviceGatherDto;
import cm.zhentao.service.TDeviceGatherService;
import cm.zhentao.service.TDeviceService;
import cm.zhentao.service.TDeviceTypeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceController {
    @Resource
    private TDeviceGatherService deviceGatherService;
    @Resource
    private TDeviceTypeService deviceTypeService;
    @Resource
    private TDeviceService deviceService;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 采集数据列表可以分页展示所有的采集数据信息。
     * 同时添加一个设备类型的下拉菜单用来做条件筛选
     * @param dto
     * @return
     */
    @PostMapping("/getGatherList")
    public Result getGatherList(@RequestBody DeviceGatherDto dto){
        Page<TDeviceGather> page = deviceGatherService.getGatherList(dto);
        return Result.OK(page);
    }

    /**
     * 设备类型信息展示
     * @return
     */
    @PostMapping("/getDeviceTypeList")
    public Result getDeviceTypeList(){
        List<TDeviceType> list = deviceTypeService.list();
        return Result.OK(list);
    }

    /**
     * 新增设备
     * @param device
     * @return
     */
    @PostMapping("/getDeviceAdd")
    public Result getDeviceAdd(@RequestBody TDevice device){
        //最小阀值
        redisTemplate.opsForValue().set("threshoidmin",device.getThreshoidmin());
        //最大阀值
        redisTemplate.opsForValue().set("threshoidmax",device.getThresholdmax());
        deviceService.save(device);
        return Result.OK();
    }

    /**
     * 设备列表
     * @param dto
     * @return
     */
    @PostMapping("/getDeviceList")
    public Result getDeviceList(@RequestBody DeviceDto dto){
        Page<TDevice> page = deviceService.getDeviceList(dto);
        return Result.OK(page);
    }
}